package com.example.imitation_webchat_demo.util;

import java.security.KeyStore;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.util.Log;

import com.example.imitation_webchat_demo.model.Resource;

/*
 * 异步资源类
 */
public class HttpClientUtils {
	// 通过异步发送数据到服务器，返回服务器的响应信息
	public static String getResponseInfo(String servletName,
			List<String> paramNames, List<Object> paramValues, Context context) {
//		HttpClient httpclient = new DefaultHttpClient();
//		String url = Resource.scheme + "://" + Resource.serverIp + ":" + Resource.serverPort
//				+ "/" + Resource.webProject + "/" + servletName;
		
		HttpClient httpclient = getNewHttpClient(context);
		String url = Resource.scheme + "://" + Resource.serverIp + ":" + Resource.httpsPort
				+ "/" + Resource.webProject + "/" + servletName;
		
		HttpPost httpPost = new HttpPost(url);
		HttpResponse response = null;
		String str = null;
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		for (int i = 0; i < paramNames.size(); i++) {
			nvps.add(new BasicNameValuePair(paramNames.get(i), paramValues.get(
					i).toString()));
		}
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
			response = httpclient.execute(httpPost);

			if (response.getStatusLine().getStatusCode() == 200) {
				str = EntityUtils.toString(response.getEntity(), "utf-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
	
	public static HttpClient getNewHttpClient(Context context) {
		try {
			// 信任所有证书的
			KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
			keyStore.load(null, null);

			SSLSocketFactory sf = new EasySSLSocketFactory(keyStore); 
			sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
			
			HttpParams params = new BasicHttpParams();
			HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); 

			SchemeRegistry registry = new SchemeRegistry();
//			registry.register(new Scheme("http", PlainSocketFactory
//					.getSocketFactory(), 180));
			registry.register(new Scheme("https", sf, Resource.httpsPort));

			ClientConnectionManager ccm = new ThreadSafeClientConnManager(
					params, registry);
			HttpClient client = new DefaultHttpClient(ccm, params); 
			return client;
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("HttpUtils", "exception");
			return new DefaultHttpClient();
		}
	}
}